package com.team622.shop.orders.service.controller;

import com.team622.shop.common.PageResult;
import com.team622.shop.common.Result;
import com.team622.shop.orders.model.entity.Payment;
import com.team622.shop.orders.service.service.PaymentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  支付记录前端控制器
 * </p>
 *
 * @author team622
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/payment")
@Tag(name = "支付记录管理", description = "支付记录的CRUD操作")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @PostMapping
    @Operation(summary = "新增支付记录", description = "创建一条新的支付记录")
    public Result<Payment> addPayment(@RequestBody Payment payment) {
        Payment savedPayment = paymentService.addPayment(payment);
        return Result.success(savedPayment);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除支付记录", description = "根据ID删除支付记录")
    public Result<Boolean> deletePayment(@Parameter(description = "支付记录ID") @PathVariable("id") Long id) {
        boolean result = paymentService.deletePayment(id);
        return Result.success(result);
    }

    @PutMapping
    @Operation(summary = "更新支付记录", description = "更新支付记录信息")
    public Result<Payment> updatePayment(@RequestBody Payment payment) {
        Payment updatedPayment = paymentService.updatePayment(payment);
        return Result.success(updatedPayment);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取支付记录详情", description = "根据ID获取支付记录详情")
    public Result<Payment> getPaymentById(@Parameter(description = "支付记录ID") @PathVariable("id")  Long id) {
        Payment payment = paymentService.getPaymentById(id);
        return Result.success(payment);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询支付记录", description = "分页查询支付记录列表，支持按订单ID、订单号和交易流水号查询")
    public PageResult<Payment> pagePayment(
            @RequestParam(value = "current", defaultValue = "1") @Parameter(description = "当前页码") Integer current,
            @RequestParam(value = "pageSize", defaultValue = "10") @Parameter(description = "每页记录数") Integer pageSize,
            @RequestParam(value = "orderId", required = false) @Parameter(description = "订单ID") Long orderId,
            @RequestParam(value = "orderNo", required = false) @Parameter(description = "订单编号") String orderNo,
            @RequestParam(value = "transactionId", required = false) @Parameter(description = "交易流水号") String transactionId
    ) {
        return PageResult.success(paymentService.pagePayment(current, pageSize, orderId, orderNo, transactionId));
    }

    @GetMapping("/order/{orderId}")
    @Operation(summary = "获取订单支付记录", description = "根据订单ID获取支付记录")
    public Result<Payment> getPaymentByOrderId(@Parameter(description = "订单ID") @PathVariable("orderId") Long orderId) {
        Payment payment = paymentService.getPaymentByOrderId(orderId);
        return Result.success(payment);
    }
}
